package com.admission.service;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.admission.dao.MessageDao;
import com.admission.model.ArrayInfo;
import com.admission.model.Message;

@Repository
@Transactional
public class MessageManager {
	private MessageDao messageDao;
	private MessageReplyManager messageReplyManager;
	private ArrayInfoManager arrayInfoManager;

	/**
	 * 保存
	 * 
	 * @return
	 */
	public void save(Message entity) {
		if (StringUtils.isEmpty(entity.getId())) {
			entity.setTag(Message.NORMAL);
			entity.setReplyTag(Message.REPLYFALSE);
		}
		messageDao.save(entity);
	}

	/**
	 * 批量删除 逗号隔开id
	 * 
	 * @return
	 */
	public void deleteByIds(String ids) {
		for (String id : ids.split(",")) {
			//messageReplyManager.deleteByMessId(id);
			messageDao.delete(id);
		}

	}

	/**
	 * 通过id找唯一对象，无返回null
	 * 
	 * @param id
	 * @return
	 */
	public Message getById(String id) {
		return messageDao.findUniqueBy("id", id);
	}

	/**
	 * 通过filter分页查询
	 * 
	 * @param page
	 * @param filters
	 * @return
	 */
	public Page<Message> pageByFilter(Page<Message> page,
			List<PropertyFilter> filters) {
		return messageDao.findPage(page, filters);
	}

	/**
	 * 获取回复不为空列表
	 * 
	 * @param page
	 * @return
	 */
	public Page<Message> pageByOnReply(final Page<Message> page) {
		String hql = "from Message m where id in (select mr.message.id from MessageReply mr) order by inputTime desc";
		return messageDao.findPage(page, hql);

	}

	/**
	 * 获取回复为空列表
	 * 
	 * @param page
	 * @return
	 */
	public Page<Message> pageByNoReply(final Page<Message> page) {
		String hql = "from Message m where id not in (select mr.message.id from MessageReply mr) order by inputTime desc";
		return messageDao.findPage(page, hql);
	}

	/**
	 * 改变留言板状态
	 * 
	 * @param id
	 * @param status
	 */
	public void changeConsult(String status) {
		arrayInfoManager.changeConsult(status, ArrayInfo.ONLINECONSULT);
	}

	/**
	 * 判断留言板权限
	 * 
	 * @return
	 */
	public String checkConsult() {
		return arrayInfoManager.checkConsult(ArrayInfo.ONLINECONSULT);
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	@Autowired
	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public MessageReplyManager getMessageReplyManager() {
		return messageReplyManager;
	}

	@Autowired
	public void setMessageReplyManager(MessageReplyManager messageReplyManager) {
		this.messageReplyManager = messageReplyManager;
	}

	public ArrayInfoManager getArrayInfoManager() {
		return arrayInfoManager;
	}

	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}

}
